<template>
  <div class="login-page">
    <!-- 登录卡片 -->
    <el-card class="login-card">
      <!-- 顶部图片 -->
      <div class="header-image-container">
        <img src="../assets/img2.png" alt="Logo" class="header-image" />
      </div>

      <!-- 登录表单 -->
      <div style="margin-right:30px ">
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-width="100px">
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                autocomplete="off"
            />
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                autocomplete="off"
            />
          </el-form-item>
          <!-- 忘记密码链接 -->
          <el-form-item>
            <a href="#" class="forgot-password">忘记密码？</a>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary" @click="handleLogin" class="submit-btn">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '../utils/request'

// 登录表单数据及校验规则
const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
const router = useRouter()

// 登录提交逻辑
async function handleLogin() {
  loginFormRef.value.validate(async (valid) => {
    if (!valid) return
    try {
      const data = await request({
        url: '/login',
        method: 'POST',
        data: {
          username: loginForm.username,
          password: loginForm.password
        }
      })
      
      ElMessage.success('登录成功')
      // 缓存用户信息到 localStorage（存储除密码外的所有信息）
      const user = {
        id: data.user.id,
        username: data.user.username,
        full_name: data.user.full_name,
        role: data.user.role,
        openid: data.user.openid,
        email: data.user.email,
        phone: data.user.phone,
        subscribed: data.user.subscribed,
        create_time: data.user.create_time,
        update_time: data.user.update_time
      }
      localStorage.setItem('user', JSON.stringify(user))
      if (data.token) {
        localStorage.setItem('token', data.token)
        // 登录成功后跳转到首页
        router.push('/home')
      } else {
        ElMessage.error('登录失败，请重试')
      }
    } catch (error) {
      ElMessage.error(error.message || '登录失败，请重试')
    }
  })
}
</script>

<style scoped>
/* 整体背景使用风景图，并加上半透明遮罩 */
.login-page {
  width: 100%;
  height: 100vh;
  background: url("../assets/img.png") no-repeat center center;
  background-size: cover;
  position: relative;
  overflow: hidden;
}

/* 添加背景遮罩，使页面内容更清晰 */
.login-page::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  z-index: 0;
}

/* 登录卡片样式 */
.login-card {
  width: 400px;
  padding: 30px 40px;
  position: absolute;
  right: 10vw;
  top: 50%;
  transform: translateY(-50%);
  background-color: #fff;
  border: 2px solid #9c27b0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(156, 39, 176, 0.4);
  z-index: 1;
}

/* 顶部图片容器 */
.header-image-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

/* 顶部图片 */
.header-image {
  max-width: 100%;
  height: auto;
}

/* 忘记密码链接 */
.forgot-password {
  float: right;
  font-size: 14px;
  color: #8e24aa;
  text-decoration: none;
  margin-top: -10px;
}

.forgot-password:hover {
  text-decoration: underline;
}

/* 按钮：使用紫色作为主色调 */
.el-button--primary {
  background-color: #8e24aa;
  border-color: #8e24aa;
}

.el-button--primary:hover {
  background-color: #7b1fa2;
  border-color: #7b1fa2;
}

/* 保持提交按钮宽度一致 */
.submit-btn {
  width: 100%;
  margin-right: 10px;
}
</style>
